import { createRouter, createWebHashHistory } from 'vue-router'

import Login from './components/user/my-login/My-Login.vue'
import Home from './components/user/my-home/My-Home.vue'
import Users from './components/menu/users.vue'
import Rights from './components/menu/rights.vue'
import Orders from './components/menu/orders.vue'
import Reset from './components/menu/reset.vue'
import Goods from './components/menu/goods.vue'
import Detail from './components/userdetail/User-Detail.vue'
const router = createRouter({
        history: createWebHashHistory(),
        routes: [
            { path: '/', redirect: '/login' },
            { path: '/login', component: Login, name: 'login' },
            {
                path: '/home',
                component: Home,
                name: 'home',
                redirect: '/home/users',
                children: [{
                        path: 'users',
                        component: Users
                    },
                    { path: 'rights', component: Rights },
                    {
                        path: 'orders',
                        component: Orders
                    },
                    { path: 'reset', component: Reset },
                    { path: 'goods', component: Goods },
                    { path: 'users/:id', component: Detail, props: true }
                ]

            },

        ]
    })
    // 设置导航守卫
router.beforeEach((to, from, next) => {
    if (to.path = 'login') next()
    const tokenStr = localStorage.getItem('token')
    if (!tokenStr) {
        next('/login')
    } else {
        next()
    }

})
export default router